package com.linglingC.service.serviceImpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.linglingC.mapper.EmplyeeMapper;
import com.linglingC.mapper.ItemChargeMapper;
import com.linglingC.mapper.PropertyItemMapper;
import com.linglingC.pojo.Emplyee;
import com.linglingC.pojo.ItemCharge;
import com.linglingC.pojo.ItemChargeAdd;
import com.linglingC.pojo.PageBean;
import com.linglingC.service.EmplyeeService;
import com.linglingC.service.ItemChargeService;
import com.linglingC.utils.ChargeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;


@Service
public class ItemChargeServiceImpl implements ItemChargeService {

    @Autowired
    private ItemChargeMapper itemChargeMapper;

    @Autowired
    private PropertyItemMapper itemMapper;

    @Override
    public PageBean findAll(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);

        List<ItemCharge> list=itemChargeMapper.findAll();

        Page<ItemCharge> p=(Page<ItemCharge>) list;

        PageBean pageBean=new PageBean(p.getTotal(),p.getResult());

        return pageBean;
    }

    @Override
    public void addItemCharge(ItemChargeAdd itemChargeAdd) {
        LocalDate start=itemChargeAdd.getStarTime();
        LocalDate end=itemChargeAdd.getEndTime();
        int day=(int)ChronoUnit.DAYS.between(itemChargeAdd.getStarTime(),itemChargeAdd.getEndTime());

        String cycle=itemMapper.findCycleByID(itemChargeAdd.getItem());
        BigDecimal bigDecimal=itemMapper.findPriceByID(itemChargeAdd.getItem());



        ItemCharge itemCharge=new ItemCharge();
        itemCharge.setRId(itemChargeAdd.getResident());
        itemCharge.setPId(itemChargeAdd.getItem());
        itemCharge.setStarTime(start);
        itemCharge.setEndTime(end);
        itemCharge.setCharge(ChargeUtil.caculate(cycle,day,bigDecimal));

        itemChargeMapper.addItemCharge(itemCharge);

    }


}
